
$(function(){
	drgUI.tips();
	$("#"+tabId+" #edate").val(drgsDate.dateTo);
	$("#"+tabId+" #sdate").val(drgsDate.dateFrom);
	//获取科室组和科室
	$.get(contextPath + "/DepGroup/findAllDepGroup.dhc").done(function(data){
		var depGroupData = data.data;
		depGroupData.unshift({"depGroupCode":"", "depGroupDesc":"全部"})
		var option = {
			onChange:function(n, o){
				var depGroupCode = n;
				$.get(contextPath + "/Department/findAllDep.dhc", {parDepGroupCode: depGroupCode}).done(function(data){
					var depData = data.data;
					depData.unshift({"depCode":"", "depName":"全部"})
					var option = {
						onChange:function(n, o){
							
						}
					}
					$CommonUI.combobox("depSelector",depData,"depName","depCode", option);
				});
			}
		}
		$CommonUI.combobox("depGroupSelector",depGroupData,"depGroupDesc","depGroupCode", option);
	});
	
	$.get(contextPath + "/Department/findAllDep.dhc").done(function(data){
		var depData = data.data;
		depData.unshift({"depCode":"", "depName":"全部"})
		var option = {
			onChange:function(n, o){
				
			}
		}
		$CommonUI.combobox("depSelector",depData,"depName","depCode", option);
	});
	//分层配置，如果科室组层不为1，隐藏科室组选择框
	if(commonLayerConfig.length>=4){
		if(commonLayerConfig[0]=="1"){
			$("#"+tabId+" #depGroupSelector").parent().css("display","block");
		}else if(commonLayerConfig[0]=="0" && commonLayerConfig[1]=="1"){
			$("#"+tabId+" #depGroupSelector").parent().css("display","none");
		}
	}
	
	$.get(contextPath + "/order/getADRGBy.dhc").done(function(data){
		var adrgDate = data.data;
		adrgDate.unshift({"CODE":"", "DESC":"全部"})
		var option = {
			onChange:function(n, o){
				$.get(contextPath + "/order/getDRGsBy.dhc", {"adrgCode": n}).done(function(data){
					var drgData = data.data;
					drgData.unshift({"CODE":"", "DESC":"全部"});
					var option = {
						onChange:function(n, o){
							
						}
					}
					$CommonUI.combobox("drgSelector",drgData,"DESC","CODE", option);
				});
			}
		}
		$CommonUI.combobox("adrgSelector",adrgDate,"DESC","CODE", option);
	});
	
	$.get(contextPath + "/order/getDRGsBy.dhc").done(function(data){
		var drgData = data.data;
		drgData.unshift({"CODE":"", "DESC":"全部"})
		var option = {
			onChange:function(n, o){
				var depGroupCode = n;
				
			}
		}
		$CommonUI.combobox("drgSelector",drgData,"DESC","CODE", option);
	});
	
	$("#searchId").click(function(){
		paramTabPage[tabId] = {
			'dateFrom': $("#"+tabId+" #sdate").val(),
			'dateTo': $("#"+tabId+" #edate").val(),
			'pType': $("#"+tabId+" input[name='pType']:checked").val(),
			'depGroupCode':$("#"+tabId+" #depGroupSelector").combobox("getValue"),
			'depCode': $("#"+tabId+" #depSelector").combobox("getValue"),
			'adrgCode': $("#"+tabId+" #adrgSelector").combobox("getValue"),
			'drgCode': $("#"+tabId+" #drgSelector").combobox("getValue")
		}
		$.post(contextPath + "/order/getOrders.dhc",paramTabPage[tabId]).done(function(data){
			$('#'+tabId+ ' .orderAnalysisResult').css({"display":"block"});
			
			paramTabPage[tabId].data1 = data.data;
			var columns=[{ "data": 'i',
				"orderData":[0,1],
				"targets":[0],
				"visible":false
			  },
              { "data": 'ddesc'},
              { "data": 'value-001-1',
            	"render": function(obj, type, full){
            		var i = full.i;
            		if(i==9){
            			if(full["value-001"]!=undefined && full["value-001"]!=0){
            				return "<a href=\"javascript:getOrderDetail('1')\">"+formatNumber(full["value-001"]);
						}else{
							return 0;
						}
            		}else if(i==13){
            			if(full["value-001"]!=undefined && full["value-001"]!=0){
            				return "<a href=\"javascript:getCases('1')\">"+formatNumber(full["value-001"]);
						}else{
							return 0;
						}
					}
            		return formatNumber(full["value-001"]);
            	}
              },
              { "data": 'value-000',
            	"render": function(obj, type, full){
            		var i = full.i;
            		if(i==9){
            			if(full["value-000"]!=undefined && full["value-000"]!=0){
            				return "<a href=\"javascript:getOrderDetail('0')\">"+formatNumber(full["value-000"]);
						}else{
							return 0;
						}
            		}else if(i==13){
            			if(full["value-000"]!=undefined && full["value-000"]!=0){
            				return "<a href=\"javascript:getCases('0')\">"+formatNumber(full["value-000"]);
						}else{
							return 0;
						}
            		}
            		return formatNumber(full["value-000"]);
            	}
              },
              { "data": 'value-all',
            	"render": function(obj, type, full){
            		var i = full.i;
            		if(i==9){
            			if(full["value-all"]!=undefined && full["value-all"]!=0){
            				return "<a href=\"javascript:getOrderDetail('')\">"+formatNumber(full["value-all"]);
						}else{
							return 0;
						}
            		}else if(i==13){
            			if(full["value-all"]!=undefined && full["value-all"]!=0){
            				return "<a href=\"javascript:getCases('')\">"+formatNumber(full["value-all"]);
						}else{
							return 0;
						}
            		}
            		return formatNumber(full["value-all"]);
            	}
              },
              { "data": 'operation',
            	"render": function(obj, type, full){
            		var i = full.i;
            		if(i==3){
            			return "<a href='javascript:feeDist()'>费用分布</a>  <a href='javascript:orderDetail(\"总费用\")'>医嘱分析</a>";
            		}
            		/*if(i==4){
            			return "<a href='javascript:feeDist()'>费用分布</a>  <a href='javascript:orderDetail(\"医疗费用\")'>医嘱分析</a>";
            		}
            		if(i==5){
            			return "<a href='javascript:feeDist()'>费用分布</a>  <a href='javascript:orderDetail(\"医技费用\")'>医嘱分析</a>";
            		}
            		if(i==6){
            			return "<a href='javascript:feeDist()'>费用分布</a>  <a href='javascript:orderDetail(\"护理费用\")'>医嘱分析</a>";
            		}
            		if(i==7){
            			return "<a href='javascript:feeDist()'>费用分布</a>  <a href='javascript:orderDetail(\"药品费用\")'>医嘱分析</a>";
            		}
            		if(i==8){
            			return "<a href='javascript:feeDist()'>费用分布</a>  <a href='javascript:orderDetail(\"管理费用\")'>医嘱分析</a>";
            		}*/
            	}
              }
             ];
			var paramsDataTables={info:false,lengthChange:false,stateSave:true,tableTitle:'医嘱分析分析结果表',searching:false,ordering:false};
			$("#"+tabId+" #orders-table").dataTableBasic(columns,data.data,paramsDataTables);
		});
	})
})
//获得病例明细
function getCases(isExceeding){
	var param = JSON.parse(JSON.stringify(paramTabPage[tabId]));
	param.isExceeding = isExceeding;
	openTab('/pages/order/orderCases.jsp', '病例明细', 'orderCases', 'orderCases', param);
}
//五大类费用饼图
function feeDist(){
	$.post(contextPath + '/pages/common/top-dialog.jsp').done(function(data){
		var dialog  = $(data);
		dialog.find(".echartscq").css({height:""});
		dialog.find(".modal-title").html("五大类费用分布");
		dialog.modal({
			keyboard: false
		}).on({
			'hidden.bs.modal' : function(){
				$(this).remove();
			},
			'shown.bs.modal' : function(){
				var str = "";
				str += "<div class='col-xs-4' id='fee1' style='width:280px;height:200px;'></div>"
				str += "<div class='col-xs-4' id='fee2' style='width:280px;height:200px;'></div>"
				str += "<div class='col-xs-4' id='fee3' style='width:280px;height:200px;'></div>"
				dialog.find(".echartscq").html(str);
				//拼接五大类数据
				var fiveFeeData = paramTabPage[tabId].data1;
				var fiveFeeDataCB=[];
				var fiveFeeDataWCB=[];
				var fiveFeeDataAll=[];
				for(var o in fiveFeeData){
					var i = fiveFeeData[o].i;
					if(i=="4"){
						var da1 = fiveFeeData[o]["value-001"];
						fiveFeeDataCB.push({name: "医疗费\n(万元)", value: da1});
						var da2 = fiveFeeData[o]["value-000"];
						fiveFeeDataWCB.push({name: "医疗费\n(万元)", value: da2});
						var da3 = fiveFeeData[o]["value-all"];
						fiveFeeDataAll.push({name: "医疗费\n(万元)", value: da3});
					}
					if(i=="5"){
						var da1 = fiveFeeData[o]["value-001"];
						fiveFeeDataCB.push({name: "医技费\n(万元)", value: da1});
						var da2 = fiveFeeData[o]["value-000"];
						fiveFeeDataWCB.push({name: "医技费\n(万元)", value: da2});
						var da3 = fiveFeeData[o]["value-all"];
						fiveFeeDataAll.push({name: "医技费\n(万元)", value: da3});
					}
					if(i=="6"){
						var da1 = fiveFeeData[o]["value-001"];
						fiveFeeDataCB.push({name: "护理费\n(万元)", value: da1});
						var da2 = fiveFeeData[o]["value-000"];
						fiveFeeDataWCB.push({name: "护理费\n(万元)", value: da2});
						var da3 = fiveFeeData[o]["value-all"];
						fiveFeeDataAll.push({name: "护理费\n(万元)", value: da3});
					}
					if(i=="7"){
						var da1 = fiveFeeData[o]["value-001"];
						fiveFeeDataCB.push({name: "药品费\n(万元)", value: da1});
						var da2 = fiveFeeData[o]["value-000"];
						fiveFeeDataWCB.push({name: "药品费\n(万元)", value: da2});
						var da3 = fiveFeeData[o]["value-all"];
						fiveFeeDataAll.push({name: "药品费\n(万元)", value: da3});
					}
					if(i=="8"){
						var da1 = fiveFeeData[o]["value-001"];
						fiveFeeDataCB.push({name: "管理费\n(万元)", value: da1});
						var da2 = fiveFeeData[o]["value-000"];
						fiveFeeDataWCB.push({name: "管理费\n(万元)", value: da2});
						var da3 = fiveFeeData[o]["value-all"];
						fiveFeeDataAll.push({name: "管理费\n(万元)", value: da3});
					}
				}
				var charParams={legendShow:false,radius:[0,'50%']};
				pieCharts(dialog.find(".echartscq").find("div#fee1"), fiveFeeDataCB, "超标病例",'',charParams);
				pieCharts(dialog.find(".echartscq").find("div#fee2"), fiveFeeDataWCB, "未超标病例",'',charParams);
				pieCharts(dialog.find(".echartscq").find("div#fee3"), fiveFeeDataAll, "全部病例",'',charParams);
			}
		});
 });
}
//医嘱项明细
function getOrderDetail(isExceeding){
	var param = JSON.parse(JSON.stringify(paramTabPage[tabId]));
	param.isExceeding = isExceeding;
	$.post(contextPath + '/pages/common/top-dialog.jsp').done(function(data){
		var dialog  = $(data);
		dialog.find(".modal-title").html("医嘱项明细");
		dialog.modal({
			keyboard: false
		}).on({
			'hidden.bs.modal' : function(){
				$(this).remove();
			},
			'shown.bs.modal' : function(){
				$.post(contextPath + "/order/getResult.dhc", param).done(function(data){
					var id = new Date().getTime();
					var str = "";
					str+=' <table id="'+id+'" class="table table-striped" ';
					str+=' cellspacing="0" width="100%" role="grid" ';
					str+=' aria-describedby="example_info"> ';
					str+=' <thead> ';
					str+=' 	<tr> ';
					str+=' 		<th>医嘱项号</th> ';
					str+=' 		<th>医嘱名称</th> ';
					str+=' 		<th>单价最大值</th> ';
					str+=' 		<th>使用人次</th> ';
					str+=' 		<th>医嘱总金额(元)</th> ';
					str+=' 		<th>平均金额(元)</th> ';
					str+=' 		<th>医嘱数量</th> ';
					str+=' 		<th>医嘱次数</th> ';
					str+=' 		<th>使用率(%)</th> ';
					str+=' 		<th>未超标人次</th> ';
					str+=' 	</tr> ';
					str+=' </thead> ';
					str+=' <tbody> ';
					str+=' </tbody> ';
					str+=' </table> ';
					dialog.find(".echartscq").html(str);
					var columns=[
									{ "data": 'ORDERID'},
									{ "data": 'ORDERDESC'},
									{ "data": 'MaxUnitPrice1',
									  "render": function(obj, type, full){
										  var maxUnitPrice = full.MAXUNITPRICE;
										  if(maxUnitPrice && parseFloat(maxUnitPrice)>500){
											  return "<label style='color:red'>"+formatNumber(maxUnitPrice)+"</label>"
										  }
										  return formatNumber(maxUnitPrice);
									  }
									},
									{ "data": 'MrCount',
									  "render": function(obj, type, full){
										  return formatNumber(full.MRCOUNT);
									  }
									},
									{ "data": 'OrderTotalFee',
										  "render": function(obj, type, full){
											  return formatNumber(full.ORDERTOTALFEE);
										  }
										},
									{ "data": 'OrderAvgFee',
									  "render": function(obj, type, full){
										  return formatNumber(full.ORDERAVGFEE);
									  }
									},
									{ "data": 'OrderQuality',
									  "render": function(obj, type, full){
										  return formatNumber(full.ORDERQUALITY);
									  }
									},
									{ "data": 'OrderCount',
									  "render": function(obj, type, full){
										  return formatNumber(full.ORDERCOUNT);
									  }
									},
									{ "data": 'UsedRate1',
									  "render": function(obj, type, full){
										  var usedRate = full.USAGERATE;
										  if(usedRate && parseFloat(usedRate)>60){
											  return "<label style='color:red'>"+usedRate+"</label>"
										  }
										  return usedRate;
									  }
									},
									{ "data": 'NotExcellent',
									  "render": function(obj, type, full){
										  return formatNumber(full.NOTEXCELLENT);
									  }
									}
				                 ];
					var paramsDataTables={info:false,stateSave:true,ordering:true,tableTitle:'医嘱项明细表',scrollY:'350px',order:[[5,'desc']]};
					var oTable = $("#"+id).dataTableBasic(columns,data.data,paramsDataTables)
					});
			}
		});
 });
}

function orderDetail(code){
	openTab('/pages/order/orderAnalysisResult.jsp', code+"|"+'分析结果', 'orderDetail', 'orderDetail', paramTabPage[tabId]);
}